package com.simple.mapper;

import com.simple.pojo.Fund;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface FundMapper extends Mapper<Fund> {

    @Delete("<script> delete from tb_fund where id in " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')' >" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void deleteByIds(List<Integer> ids);

    @Select("<script> select f.* from tb_fund f " +
            "right JOIN " +
            "(select " +
            "tf.fund_name,max(create_time) as create_time from tb_fund tf " +
            "where 1 = 1 " +
            "<if test='userId != null and userId != -1'> and tf.user_id = #{userId} </if> " +
            "<if test='location != null and location != -1'> and tf.location = #{location} </if> " +
            "<if test='fundName != null and fundName != \"\" '> and tf.fund_name like concat('%',concat(#{fundName},'%')) </if> " +
            "GROUP BY tf.fund_name) t " +
            "on f.fund_name = t.fund_name and f.create_time = t.create_time " +
            " </script>")
    List<Fund> getFunds(@Param("userId") Integer userId,@Param("fundName") String fundName,@Param("location")Integer location);

    @Select("select * from tb_fund where fund_name = #{fundName} order by create_time DESC limit 2")
    List<Fund> get2LastestTimeByFundName(String fundName);

    @Select("select f.* from (select fund_name, max(create_time) as time from (select * from tb_fund where user_id = #{userId}) t " +
            "GROUP BY fund_name, MONTH(create_time)) t " +
            "left join tb_fund f " +
            "on t.fund_name = f.fund_name and t.time=f.create_time")
    List<Fund> getFundsOfMonth(Integer userId);

    @Select("<script> select * from tb_fund where id in " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')' >" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Fund> getFundsByIds(List<Integer> ids);

    /**
     * 获取所有的基金
     */
    @Select("select * from tb_fund " +
            "where user_id = #{userId} and fund_name = #{fundName} and DATE_FORMAT(create_time,'%Y') = YEAR(CURRENT_DATE) " +
            "order by update_time asc")
    List<Fund> getAllFunds(@Param("fundName")String fundName,@Param("userId")Integer userId);

}